/*	
   ______   ______    
  /\__  __\/\ \___\ 
  \/_/\ \_/\ \ \__/    __   __     __       
     \ \ \  \ \ \__\  /\ \ /\ \  /'__`\  
     _\_\ \  \ \ \_/_ \ \ \\_\ \/\  __/       
    /\____/   \ \_____\\ \_____/\ \____\   
    \_/__/     \/_____/ \/____/  \/____/                   
 
 	This file is part of the JEVE application API.

    JEve is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    JEve is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Copyright 2008 JEve Project (JEveProject@gmail.com)
 */
package be.fomp.jeve.model.data.standard.alliancelist;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.Element;

import be.fomp.jeve.model.data.standard.AllianceList.Alliance;
import be.fomp.jeve.model.util.DateParser;
/**
 * <pre>
 *  ______   ______
 * /\__  __\/\ \___\
 * \/_/\ \_/\ \ \__/    __   __     __
 *    \ \ \  \ \ \__\  /\ \ /\ \  /'__`\
 *    _\_\ \  \ \ \_/_ \ \ \\_\ \/\  __/
 *   /\____/   \ \_____\\ \_____/\ \____\
 *   \_/__/     \/_____/ \/____/  \/____/
 * </pre>
 * This file is part of the JEVE application API.<br />
 * <br />
 * <p/>
 * Implementation of the AllianceTemplate Interface.
 * 
 * @author Sven Meys
 */
final class AllianceImpl implements Alliance{
	static final long serialVersionUID = 1L;
	
	/** The Alliance name */
	private String name;
	
	/** The alliance Shortname */
	private String shortName;
	
	/** The alliance ID */
	private Integer allianceID;
	
	/** The ID of the corporation that started the alliance */
	private Integer executorCorpID;
	
	/**  The ammount of member corporations within the alliance */
	private Integer memberCount;
	
	/** The start date of the alliance */
	private Date startDate;
	
	/** The list of member corporations in the alliance */
	private List<MemberCorporation> memberCorporations = new ArrayList<MemberCorporation>();
	
	/**
	 * Main and only constructor.<br />
	 * Takes a JDOM Element object and stores the needed values 
	 * into the instance variables.<br />
	 * @param alliance The JDOM elemen containing alliance data.
	 */
	@SuppressWarnings("unchecked")
	AllianceImpl(Element alliance) {
		
		this.allianceID  = Integer.valueOf(alliance.getAttributeValue("allianceID"));
		
		this.name = alliance.getAttributeValue("name");
		this.shortName = alliance.getAttributeValue("shortName");
		this.executorCorpID = Integer.valueOf(alliance.getAttributeValue("executorCorpID"));
		this.memberCount = Integer.valueOf(alliance.getAttributeValue("memberCount"));
		this.startDate = DateParser.parseDate(alliance.getAttributeValue("startDate"));
		
		List<Element> corporations = (List<Element>)alliance.getChild("rowset").getChildren();
		
		for(Element corporationElement: corporations)
		{
			MemberCorporation memberCorporation = new MemberCorporationImpl(corporationElement);

			this.memberCorporations.add( memberCorporation);
		}
	}

	public String getName() {return this.name;}
	public String getShortName() {return this.shortName;}
	public Integer getAllianceID() {return this.allianceID;}
	public Integer getExecutorCorpID() {return this.executorCorpID;}
	public Integer getMemberCount() {return this.memberCount;}
	public Date getStartDate() {return this.startDate;}

	public List<MemberCorporation> getMemberCorporations() {
		return this.memberCorporations;
	}
	
	/**
	 * Needed for serialization
	 * @param os The stream to serialize to
	 */
	public void writeObject(ObjectOutputStream os) {
		try {
				os.writeObject(this.memberCorporations);
		} catch (Exception e) { e.printStackTrace();  }
	}
	
	/**
	 * Needed for serialization
	 * @param is The stram to deserialize from
	 */
	@SuppressWarnings("unchecked")
	public void readObject (ObjectInputStream is) {
		try {
			is.defaultReadObject();
			this.memberCorporations = (List<MemberCorporation>)is.readObject();
		} catch (Exception e) {e.printStackTrace();}
		
	}
}
